package com.onpositive.datamap.core.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import com.onpositive.data.IReadableObjectCollection;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.IDataObject;
import com.onpositive.datamap.core.IDataProperty;
import com.onpositive.datamap.core.IImageObject;
import com.onpositive.datamap.core.ITextSection;
import com.onpositive.datamap.core.ITypedReference;
import com.onpositive.datamap.core.IVideoObject;
import com.onpositive.datamap.values.impl.TextSectionModel;
import com.onpositive.picture.utils.YoutubeFeed;
import com.onpositive.semantic.model.api.property.IPropertyProvider;

public class DataObject implements IDataObject{
	
	DataCollection collection;
	public final int num;

	public DataObject(DataCollection dataCollection, int num) {
		this.collection=dataCollection;
		this.num=num;
	}

	@Override
	public String getUID() {
		return collection.getString(num,collection.uid_prop).replace('/', '-');
	}

	@Override
	public String getCaption() {
		return collection.getString(num,collection.name_prop);
	}

	@Override
	public IDataPropertyValue[] getProperties() {
		IDataProperty[] properties = collection.getProperties();
		ArrayList<IDataPropertyValue>vl=new ArrayList<IDataPropertyValue>();
		for (IDataProperty m:properties){
			IDataPropertyValue value = m.getValue(this);
			if (value!=null){
				vl.add(value);
			}
		}
		return vl.toArray(new IDataPropertyValue[vl.size()]);
	}

	@Override
	public ITextSection getTextDescription() {
		if (collection.text_prop==null){
			return null;
		}
		byte[] bytes = collection.getBytes(num, collection.text_prop);
		if (bytes!=null){
			return parseSection(bytes);
		}
		return  null;
	}
	public String getTextContentAsString(){
		if (collection.text_prop==null){
			return null;
		}
		byte[] bytes = collection.getBytes(num, collection.text_prop);
		if (bytes!=null){
			try {
				return new String(bytes,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				
			}
		}
		return "";
	}

	private ITextSection parseSection(byte[] bytes) {
		try {
			return new TextSectionModel(new String(bytes,"UTF-8"),-1);
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public IImageObject[] getGalleryImages() {
		if (collection.imageLinkProperty==null){
			return new IImageObject[0];
		}
		ArrayList<String> value = (ArrayList<String>) collection.collection.getValue(this.num, collection.imageLinkProperty);
		if (value!=null){
			ArrayList<String> cm = (ArrayList<String>) collection.collection.getValue(this.num, collection.imageCommentProperty);
			IImageObject[] rs=new IImageObject[value.size()];
			for (int a=0;a<value.size();a++){
				rs[a]=new LinkImageObject(value.get(a),cm.get(a));
			}
			return rs;
		}
		return new IImageObject[0];
	}

	@Override
	public IImageObject getPrimaryImage() {
		if (collection.mainImage!=null){
			return new InternalImageObject(this.num,collection.mainImage,(IReadableObjectCollection<?>) collection.collection);
		}
		return null;
	}

	@Override
	public IImageObject getPreviewImage() {
		if (collection.previewImage!=null){
			return new InternalImageObject(this.num,collection.previewImage,(IReadableObjectCollection<?>) collection.collection);
		}
		return null;
	}

	@Override
	public boolean hasImage() {
		if (collection.imageLinkProperty==null){
			return false;
		}
		String[] value = (String[]) collection.collection.getValue(this.num, collection.imageLinkProperty);
		return value!=null&&value.length>0;
	}

	@Override
	public boolean hasVideos() {
		return true;
	}

	@Override
	public IVideoObject[] getVideos() {
		ArrayList<IVideoObject> build = YoutubeFeed.build(getCaption());
		return build.toArray(new IVideoObject[build.size()]);
	}

	@Override
	public IPropertyProvider getPropertyProvider() {
		return collection;
	}

	@Override
	public String toString() {
		return getCaption();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((collection == null) ? 0 : collection.hashCode());
		result = prime * result + num;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DataObject other = (DataObject) obj;
		if (collection == null) {
			if (other.collection != null)
				return false;
		} else if (!collection.equals(other.collection))
			return false;
		if (num != other.num)
			return false;
		return true;
	}

	@Override
	public <C> Collection<ITypedReference<C>> getRelated(Class<C> c) {
		return Collections.emptySet();
	}

	@Override
	public boolean isPrimaryObject() {
		return collection.isActual(num);
	}
	
	
	public void setPrimaryObject(boolean value){
		collection.setActual(num,value);
	}
}
